package com.lc.ibps.platform.rest.bpmn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.core.util.BeanUtils;
import com.lc.ibps.bpmn.api.model.image.BpmProcessStatusColor;
import com.lc.ibps.bpmn.api.model.image.FlowImageStyle;
import com.lc.ibps.bpmn.api.model.inst.IBpmProcInst;
import com.lc.ibps.bpmn.api.model.task.IBpmTask;
import com.lc.ibps.bpmn.api.service.BpmProcStatusService;
import com.lc.ibps.bpmn.api.service.BpmTaskService;
import com.lc.ibps.bpmn.persistence.entity.BpmDefineXmlPo;
import com.lc.ibps.bpmn.repository.BpmDefineXmlRepository;
import com.lc.ibps.bpmn.repository.BpmInstRepository;
import com.lc.ibps.bpmn.utils.FlowStatusColorUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 流程定义 控制类
 *
 * <pre>
 *  
 * 开发公司：广州流辰信息技术有限公司
 * 开发人员：simon cai
 * 邮箱地址：caij@bpmhome.cn
 * 创建时间：2017-08-30 9:37:25
 * </pre>
 */
@RequestMapping(value = "/api/webapi/bpmImgService")
@Api(value = "/bpmImgService", tags = "流程管理-流程图服务")
@RestController("bpmImgRest")
public class BpmImgController extends BaseApiController {

	private BpmDefineXmlRepository bpmDefineXmlRepository;
	private BpmTaskService bpmTaskService;
	private BpmInstRepository bpmInstRepository;
	private BpmProcStatusService flowStatusService;

	public BpmImgController() {
		bpmDefineXmlRepository = AppUtil.getBean(BpmDefineXmlRepository.class);
		bpmTaskService = AppUtil.getBean(BpmTaskService.class);
		bpmInstRepository = AppUtil.getBean(BpmInstRepository.class);
		flowStatusService = AppUtil.getBean(BpmProcStatusService.class);
	}


	@ApiOperation(value = "流程图", notes = "流程图")
	@RequestMapping(value = "/getFlowDiagram", method = RequestMethod.GET)
	public APIResult<Map<String, Object>> getFlowDiagram(
			@RequestParam(name = "defId", required = false) @ApiParam(name = "defId", value = "defId", required = false) String defId,
			@RequestParam(name = "bpmnInstId", required = false) @ApiParam(name = "bpmnInstId", value = "bpmnInstId", required = false) String bpmnInstId,
			@RequestParam(name = "taskId", required = false) @ApiParam(name = "taskId", value = "taskId", required = false) String taskId) {

		APIResult<Map<String, Object>> result = new APIResult<>();
		try {
			Map<String, FlowImageStyle> styleMap = new HashMap<String, FlowImageStyle>();
			List<BpmProcessStatusColor> statusColorList = new ArrayList<BpmProcessStatusColor>();
			if (BeanUtils.isNotEmpty(taskId)) {
				IBpmTask task = bpmTaskService.getByTaskId(taskId);
				defId = task.getProcDefId();

				styleMap = flowStatusService.getProcInstStatus(task.getProcInstId());
				statusColorList  = FlowStatusColorUtil.getProcessStatusColorList();
			} else if (BeanUtils.isNotEmpty(bpmnInstId)) {
				// 获取正在运行的流程实例信息
				IBpmProcInst bpmProcessInstance = bpmInstRepository.get(bpmnInstId);
				if (BeanUtils.isEmpty(bpmProcessInstance)) {
					// 获取结束的流程实例信息
					bpmProcessInstance = bpmInstRepository.getHistoryById(bpmnInstId);
				}
				if (BeanUtils.isNotEmpty(bpmProcessInstance)) {
					defId = bpmProcessInstance.getProcDefId();
				}

				styleMap = flowStatusService.getProcInstStatus(bpmnInstId);
				statusColorList  = FlowStatusColorUtil.getProcessStatusColorList();
			}
			BpmDefineXmlPo po = bpmDefineXmlRepository.get(defId);

			
			
			result.setData(	
					b()
					.a("xml", BeanUtils.isNotEmpty(po)? po.getDefXml().replace("xns=\"", "xmlns=\"").replaceAll("&#10;", "").replaceAll("&#13;", "") : "")
					.a("styleMap", styleMap)
					.a("statusColorList", statusColorList)
					.p());
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取流程图失败", e);
		}
		return result;
	}

}
